package com.yingdong.centerserver.net;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yingdong.centerserver.inner.message.GameMessage;
import com.yingdong.centerserver.opcode.Dispatcher;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class CenterServerHandlerNetty4 extends ChannelInboundHandlerAdapter {
	static Log logger = LogFactory.getLog(CenterServerHandlerNetty4.class);

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		for (int key : Dispatcher.centerServerSessionMap.keySet()) {
			Channel channel = Dispatcher.centerServerSessionMap.get(key);
			if (channel == ctx.channel()) {
				Dispatcher.centerServerSessionMap.remove(key);
			}
		}
		// logger.info("连接中断");
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf buffer = (ByteBuf) msg;
		try {
			Dispatcher.receiveBuffers.put(new GameMessage(buffer, ctx.channel()));

		} catch (Exception e1) {

			logger.debug("", e1);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// logger.info("", cause);
	}

}
